package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.domain.Activity;
import cn.leyaber.hrm.query.ActivityQuery;
import cn.leyaber.hrm.service.IActivityService;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    public IActivityService activityService;
    //批量删除
    @PatchMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long>dis ){
        try {
            activityService.batchDelete(dis);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("全部删除失败！"+e.getMessage());
        }
    }
    //修改状态
   @PostMapping("Modify_status")
    public AjaxResult Modify_status(@RequestBody Activity activity){
        try {
            activityService.Modify_status(activity);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改状态失败！"+e.getMessage());
        }
    }


    /**
    * 保存和修改公用的
    * @param activity  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Activity activity){
        try {
            if(activity.getId()!=null){
                activityService.updateById(activity);
            }else{
                activityService.insert(activity);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            activityService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Activity get(@PathVariable("id")Long id)
    {
        return activityService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Activity> list(){

        return activityService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Activity> json(@RequestBody ActivityQuery query)
    {
        Page<Activity> page = new Page<Activity>(query.getPage(),query.getRows());
        page = activityService.selectPage(page);
        return new PageList<Activity>(page.getTotal(),page.getRecords());
    }
}
